'use strict';
module.exports = {
	/**
	 * 注销账户
	 * @url client/setting/kh/deleteUser 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		if (uid == '001') return {
			code: -1,
			msg: '无法删除超级管理员'
		}

		// 删除地址
		await vk.baseDao.del({
			dbName: "address",
			whereJson: { uid }
		})

		// 删除帖子相关
		await vk.baseDao.del({
			dbName: "bbs_article",
			whereJson: { uid }
		})
		await vk.baseDao.del({
			dbName: "user-bbs-star",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "user-bbs-tumbup",
			whereJson: { user_id: uid }
		})


		// 删除消息
		await vk.baseDao.del({
			dbName: "msg",
			whereJson: { uid }
		})

		// 删除答题相关
		await vk.baseDao.del({
			dbName: "q_answercard",
			whereJson: { uid }
		})
		await vk.baseDao.del({
			dbName: "q_answercard_his",
			whereJson: { uid }
		})
		await vk.baseDao.del({
			dbName: "q_user_count",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "q_user_error",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "q_user_everyday_practice",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "q_user_simulation",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "q_user_star",
			whereJson: { user_id: uid }
		})

		//删除课程相关
		await vk.baseDao.del({
			dbName: "user_video_watch_records",
			whereJson: { user_id: uid }
		})
		await vk.baseDao.del({
			dbName: "user-star",
			whereJson: { user_id: uid }
		})

		// 执行数据库API请求
		res.num = await vk.baseDao.deleteById({
			dbName: "uni-id-users",
			id: uid
		})

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
